package com.friend.joey.applicationfriend.Adapters;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.friend.joey.applicationfriend.R;
import com.friend.joey.applicationfriend.Classes.Result;

import java.util.ArrayList;


/**
 * Created by Joey on 2017/5/28.
 */

public class SearchAdaper extends RecyclerView.Adapter<SearchAdaper.ViewHolder> {
    public static final int TYPE_NOMAL=0;
    public static final int TYPE_MUSIC=1;
    public static final int TYPE_INFO=2;
    private ArrayList<Result> SearchList;
    private onSearchCallback mCallback;
    private int type;

    public interface onSearchCallback {
        void onDeleteclick(int itempostion);
        void onShowclick(int itempostion);
    }

    public void setOnSearchCallback(onSearchCallback callBack){
        mCallback=callBack;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;
        CardView cardView;
        Button delete;
        Button play;
        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.search_title);
            content = (TextView) view.findViewById(R.id.search_content);
            cardView=(CardView)view.findViewById(R.id.search_cardView);
            delete=(Button)view.findViewById(R.id.button_childdelete);
            play=(Button)view.findViewById(R.id.button_musicplay);
        }
    }

    public SearchAdaper(ArrayList<Result> remList) {
        SearchList = remList;
        this.type=TYPE_NOMAL;
    }
    public SearchAdaper(ArrayList<Result> remList,int type) {
        SearchList = remList;
        this.type=type;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fsresult_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCallback != null) {
                    mCallback.onShowclick((Integer) view.getTag());
                }
            }
        });
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Result result = SearchList.get(position);
        holder.title.setText(result.getTitle());
        holder.content.setText(result.getSnippet());
        if(type==TYPE_INFO){
            holder.delete.setVisibility(View.VISIBLE);
        }
        if(type==TYPE_MUSIC){
            holder.play.setVisibility(View.VISIBLE);
            holder.play.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        return SearchList.size();
    }
}
